package com.bruce.cloud.feign.service;

import com.bruce.cloud.feign.api.Page;
import com.bruce.cloud.feign.api.Pageable;
import com.bruce.cloud.feign.api.Store;
import com.bruce.cloud.feign.api.StoreClient;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;
import java.util.Map;

/**
 * Created by bruce in 2025/6/17 02:15
 */
@RestController
@RequestMapping("/service")
public class StoreClientController implements StoreClient {
    private static final Logger log = LoggerFactory.getLogger(StoreClientController.class);

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public List<Store> getStores() {
        Store store = new Store();
        store.setName("bruce");
        store.setData("哈哈");
        return List.of(store);
    }

    @Override
    public Page<Store> queryStores(Pageable pageable) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();

        String requestURI = request.getRequestURI();
        String queryString = request.getQueryString();

        log.info("requestURI:{}, queryString:{}", requestURI, queryString);

        Store store = new Store();
        store.setName("queryStores");
        store.setData("哈哈");
        Page<Store> objectPage = new Page<>();
        objectPage.setData(store);

        log.info("queryStores: pageable:{}", pageable);

        return objectPage;
    }

    @Override
    public Store update(Long storeId, Store store) {
        return null;
    }

    @Override
    public void delete(Long storeId) {

    }

    @Override
    public Store save(Store store) {
        return store;
    }

    @Override
    public Store saveForm(Store store) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String requestURI = request.getRequestURI();
        String queryString = request.getQueryString();

        log.info("saveForm: store:{}, requestURI:{}, queryString:{}", store, requestURI, queryString);

        return store;
    }

    @Override
    public Store requestMap(Map<String, String> params) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        Map<String, String[]> parameterMap = request.getParameterMap();

        try {
            log.info("requestMap: params:{}, HttpServletRequest parameterMap:{}", params, objectMapper.writeValueAsString(parameterMap));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return new Store(params.get("name"), params.get("age"));
    }
}
